package org.lanqiao.controller.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.lanqiao.admin.IAdministratorService;
import org.lanqiao.admins.Administrator;
import org.lanqiao.config.entity.ResponseData;
import org.lanqiao.users.Admin;
import org.lanqiao.utils.JWTUtil;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage/admin")
@Api(tags = "操作管理员接口")
public class AdministratorController {
    @Reference
    private IAdministratorService adminService;


    @GetMapping("/login")
    @ApiOperation(value = "管理登陆接口",notes = "用户名和密码认证")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "账号",paramType ="query",required = true,name = "loginname",dataType = "String"),
            @ApiImplicitParam(value = "密码",paramType ="query",required = true,name = "pass",dataType = "String")
    })
    public ResponseData<Administrator> login(@RequestParam String loginname, @RequestParam String pass, HttpSession session){
        ResponseData responseData = null;
        //根据用户名查询用户信息
        Administrator administrator = adminService.queryAdminByName(loginname);
        if(administrator == null){
            responseData = ResponseData.cusstomerError("用户不存在");
        }else{
            boolean checkPw =  BCrypt.checkpw(pass,administrator.getPassword());
            if(checkPw){
                String token =  JWTUtil.generToken(administrator.getId().toString(),"LiuLiu@Mall",administrator.getName());
                responseData = ResponseData.success().putDataVule("token",token).putDataVule("administrator",administrator);
                session.setAttribute("id",administrator.getId());
                System.out.println(session.getAttribute("id"));
                responseData = ResponseData.success().putDataVule("token",token).putDataVule("administrator",administrator).putDataVule("session",session);
            }else{
                responseData = ResponseData.cusstomerError("管理认证失败");
            }
        }
        return responseData;
    }
    @Reference
    private IAdministratorService iAdministratorService;
    @GetMapping("/administratorList")
    //此方法用来获取所有管理员列表,不需要传参数
    @ApiOperation(value="管理员列表",notes="获取所有管理员")
    public ResponseData<Administrator> administratorList(){
        List<Administrator> administratorList = iAdministratorService.administratorList();
        if(administratorList!= null){
            return ResponseData.success().putDataVule("administratorlist",administratorList);
        }else{
            return ResponseData.serverInternalError();
        }
    }

    //此方法根据条件查询相应的管理员
    //param
    //    1:queryMap 存放查询条件的map
    //       queryMap中的参数--1:name  根据管理员姓名模糊查询
    //       queryMap中的参数--2: status  根据状态查询管理员 1:正常 0:禁用
    //       queryMap中的参数--3: role  根据角色查询管理员 1:超级管理员 2:普通管理员
     @PostMapping("/administratorQuery")
    @ApiOperation(value="条件查询管理员列表",notes="根据条件查询相应的管理员")
    @ApiImplicitParam(value = "查询条件" ,name="queryMap" ,required = false,dataType = "Map",paramType = "body")
    public ResponseData<Administrator> administratorlistBycondition(@RequestBody Map<String ,Object> queryMap){
        List<Administrator> administratorList = iAdministratorService.administratorListByCondition(queryMap);
        if(administratorList!= null){
            return ResponseData.success().putDataVule("administratorlist",administratorList);
        }else{
            return ResponseData.serverInternalError();
        }
    }

    //此方法分页查询所有管理员
    //param
    //    1:pagenum 当前页数
    //    2:pagesize 每页有多少数据
    @GetMapping("/administratorPage")
    @ApiOperation(value = "分页查询",notes = "分页查询所有管理员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页数",name = "pagenum",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "pagesize",required = false,dataType = "Integer",paramType = "query")
    })
    public ResponseData<Administrator> bradPage(@RequestParam  Integer pagenum,@RequestParam Integer pagesize){
        IPage<Administrator> administratorPage =  iAdministratorService.administratorPage(pagenum,pagesize);
        return ResponseData.success().putDataVule("totla",administratorPage.getTotal())
                .putDataVule("pages",administratorPage.getPages())
                .putDataVule("brandList",administratorPage.getRecords());

    }

    //此方法根据条件分页查询所有管理员
    //param
    //    1:pagenum 当前页数
    //    2:pagesize 每页有多少数据
    //    3:queryMap 存放查询条件的map
    //       queryMap中的参数--1:name  根据管理员姓名模糊查询
    //       queryMap中的参数--2: status  根据状态查询管理员 1:正常 0:禁用
    //       queryMap中的参数--3: role  根据角色查询管理员 1:超级管理员 2:普通管理员
    @PostMapping("/administratorPageByCondition")
    @ApiOperation(value = "分页条件查询",notes = "根据条件分页查询管理员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页数",name = "pagenum",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "pagesize",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "查询条件",name = "queryMap",required = false,dataType = "Map",paramType = "body")
    })
    public  ResponseData<Administrator> brandPageQuery(@RequestParam  Integer pagenum,@RequestParam Integer pagesize,@RequestBody Map<String,Object> queryMap){
        IPage<Administrator> administratorPage =  iAdministratorService.administratorPageByCondition(pagenum,pagesize,queryMap);
        return ResponseData.success().putDataVule("totla",administratorPage.getTotal())
                .putDataVule("pages",administratorPage.getPages())
                .putDataVule("brandList",administratorPage.getRecords());
    }

    //此方法用来增加管理员
    //param
    //    administrator:对应数据库中的ws_admin
    //说明:传administrator的数据可以不用传id,因为id自增,另外因为管理员姓名被设置为唯一索引,所以如果传入管理员姓名重复会返回-1,成功插入则返回1
    @PostMapping("/addAdministrator")
    @ApiOperation(value="增加管理员",notes="增加相应的管理员信息")
    @ApiImplicitParam(value = "增加管理员信息" ,name="administrator" ,required = false,dataType = "Administrator",paramType = "body")
    public ResponseData<Administrator> addAdministrator(@RequestBody Administrator administrator){
       Integer res = iAdministratorService.addAdministrator(administrator);
        if(res == 1){
            return ResponseData.success();
        }else{
            return ResponseData.serverInternalError();
        }
    }

    //此方法用来更新管理员信息
    //param
    //    administrator:对应数据库中的ws_admin
    //说明:成功更新返回1
    @PostMapping("/updateAdministrator")
    @ApiOperation(value="更新某个管理员",notes="更新某个管理员的信息")
    @ApiImplicitParam(value = "更新管理员信息" ,name="administrator" ,required = false,dataType = "Administrator",paramType = "body")
    public ResponseData<Administrator> updateAdmintrator(@RequestBody Administrator administrator){
        Integer res = iAdministratorService.updateAdministrator(administrator);
        if(res == 1){
            return ResponseData.success();
        }else{
            return ResponseData.serverInternalError();
        }
    }

    //此方法用来删除管理员
    //param
    //    id:对应数据库中的ws_admin中的id
    //说明:成功删除返回1
    @GetMapping("/deleteAdministrator")
    @ApiOperation(value="删除某个管理员",notes="根据id删除某个管理员")
    @ApiImplicitParam(value = "删除管理员" ,name="id" ,required = false,dataType = "Integer",paramType = "query")
    public ResponseData<Administrator> deleteAdmintrator(@RequestParam Integer id){
        Integer res = iAdministratorService.deleteAdministratorById(id);
        if(res == 1){
            return ResponseData.success();
        }else{
            return ResponseData.serverInternalError();
        }
    }
}
